/* eslint-disable prettier/prettier */
import config from './config.js'
import { message } from 'antd'

const { ipcRenderer, shell } = require('electron')

/**
 * loading
 * @param {*} ctx
 * @param {*} isShow
 */
const loading = (ctx, isShow) => {
  if (ctx) {
    ctx.setMaskShow(isShow)
  }
}

/**
 * 打开浏览器
 * @param {*} e
 * @param {*} url
 */
const openBrowser = (e, url) => {
  e.preventDefault()
  shell.openExternal(url).then(() => {})
}

/**
 * 错误提示
 * @param {*} ctx
 */
const errorMsg = (ctx) => {
  message.open({
    type: 'error',
    content: ctx.t('optErr'),
    duration: 1.5
  })
}

/**
 * 错误提示
 * @param msg
 */
const errorMsg2 = (msg) => {
  message.open({
    type: 'error',
    content: msg,
    duration: 1.5
  })
}

/**
 * 成功提示
 * @param {*} ctx
 */
const successMsg = (ctx) => {
  message.open({
    type: 'success',
    content: ctx.t('optOk'),
    duration: 1.5
  })
}

/**
 * 成功提示
 * @param msg
 */
const successMsg2 = (msg) => {
  message.open({
    type: 'success',
    content: msg,
    duration: 1.5
  })
}

/**
 * 警告提示
 */
const warnMsg = (msg) => {
  message.open({
    type: 'warning',
    content: msg,
    duration: 2
  })
}

/**
 * 提示信息
 * @param {*} msg
 */
const infoMsg = (msg) => {
  message.open({
    type: 'info',
    content: msg
    // duration: 2
  })
}

/**
 * 设置语言
 * @returns
 */
const defaultLng = () => {
  const settings = config.getData()
  return settings.lng === 'zh'
}

/**
 * 最小化窗口、关闭窗口
 * @param {*} opt 最小化: winMini; 最大化: winClose
 */
const winOpt = (opt) => {
  ipcRenderer.send(opt)
}

/**
 * 切换语言
 * @param {*} ctx
 * @param {*} checked
 */
const switchLng = (ctx, checked) => {
  const lng = checked ? 'zh' : 'en'
  ctx.changeLanguage(lng)
  let settings = config.getData()
  settings.lng = lng
  config.writeData(JSON.stringify(settings))
}

/**
 * 获取当前语言信息
 * @returns {any}
 */
const getLng = () => {
  return config.getData().lng
}

export {
  loading,
  openBrowser,
  errorMsg,
  errorMsg2,
  successMsg,
  successMsg2,
  warnMsg,
  infoMsg,
  defaultLng,
  winOpt,
  switchLng,
  getLng
}
